package com.gene.controller.content;

import com.gene.pojo.TbContent;
import com.gene.service.ITbContentService;
import com.gene.utils.PageResult;
import com.gene.utils.ResultData;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 内容详情
 */
@Controller
@RequestMapping("/gene-safe/content")
public class TbContentController {

    @RequestMapping("index")
    public String index(){
        return "index";
    }

    @Autowired
    private ITbContentService tbContentService;
    /**
     * 添加一个内容
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save")
    public ResultData save(TbContent tbContent){
        ResultData result=new ResultData();
        boolean bool=tbContentService.save(tbContent);
        if (bool){
            result.setStatus("1");
            result.setMsg("添加成功...");
        }else{
            result.setStatus("0");
            result.setMsg("添加失败！");
        }
        return result;
    }
    /**
     * 修改类容
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ResultData update(TbContent tbContent){
        ResultData result=new ResultData();
        boolean bool=tbContentService.update(tbContent);
        if (bool){
            result.setStatus("1");
            result.setMsg("修改成功...");
        }else{
            result.setStatus("0");
            result.setMsg("修改失败！");
        }
        return result;
    }

    /**
     * 删除
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public ResultData del(@RequestParam("id")Integer id){
        ResultData result=new ResultData();
        Boolean bool=tbContentService.del(id);
        if (bool){
            result.setStatus("1");
            result.setMsg("删除成功...");
        }else{
            result.setStatus("0");
            result.setMsg("删除失败！");
        }
        return result;
    }
    /**
     *内容查询分页
     */
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    @ResponseBody
    public PageResult query(@RequestParam(defaultValue = "1")Integer pageNum,
                            @RequestParam(defaultValue = "10")Integer pageSize,TbContent tbContent){
        PageResult result=new PageResult();
        PageInfo page=tbContentService.query(pageNum,pageSize,tbContent);
        if(page.getList().size()>0){
            result.setPageSize(page.getPageSize());//每页多少数
            result.setPageNumber(page.getPageNum());//当前页
            result.setTotalPage(page.getPages());//共多少页
            result.setTotalRows((int)page.getTotal());//总数据量
            result.setList(page.getList());//结果集
            result.setMsg("查询成功！");
        }else{
            result.setMsg("没有数据！");
        }
        return result;
    }

    /**
     * 查看详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/content",method = RequestMethod.POST)
    @ResponseBody
    public ResultData content(@RequestParam("id")Integer id){
        ResultData result=new ResultData();
        TbContent tbContent=tbContentService.content(id);
        if (null!=tbContent){
            result.setData(tbContent);
            result.setStatus("1");
            result.setMsg("查询成功！");
        }else{
            result.setStatus("0");
            result.setMsg("没有数据！");
        }
        return  result;
    }
}
